//
//  MapController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/4/6.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MapController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapController ()<UITextFieldDelegate,CLLocationManagerDelegate,MKMapViewDelegate>
////维度
//@property (nonatomic, strong)UITextField *latitudeTextField;
////经度
//@property (nonatomic, strong)UITextField *longtitudeTextField;
////地图
@property (nonatomic, strong)MKMapView *currentMapView;
//当前位置
//@property (nonatomic, strong)UITextField *currentAddressTextField;

@property(nonatomic,strong)CLLocationManager * locationManager;

@end

@implementation MapController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initLayout];

    //初始化locationManager
    self.locationManager = [[CLLocationManager alloc]init];
    //添加判断是否是iOS8以前版本   在info.plist中添加NSLocationWhenInUseUsageDescription字段
    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
        // 始终定位
        [self.locationManager requestAlwaysAuthorization];
        //使用时定位
        //        [self.locationManager requestWhenInUseAuthorization];
    }
    //设置代理
    self.locationManager.delegate = self;
    //开启定位
    [self.locationManager startUpdatingLocation];
    
    
    //对MKMap进行初始化,添加代理方法后就不需要了
    //    [MKMapView initialize];
    
    
    self.currentMapView.mapType = MKMapTypeHybridFlyover;
    self.currentMapView.delegate = self;
    //跟随状态
    self.currentMapView.userTrackingMode = MKUserTrackingModeFollow;
    //是否显示当前位置
    self.currentMapView.showsUserLocation = YES;
    
}

- (void)initLayout{
    self.currentMapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:self.currentMapView];
    
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
    //自定义大头针
    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
    pointAnnotation.coordinate = userLocation.location.coordinate;
    
//    pointAnnotation.title = @"北京市";
//    pointAnnotation.subtitle = self.currentAddressTextField.text;
    
    [mapView addAnnotation:pointAnnotation];
    
}

#pragma mark--------CLLocationManagerDelegate方法-------

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//    NSLog(@"定位失败%@",error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
//    NSLog(@"定位成功");
    CLLocation *currentLocation = locations[0];

    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    //逆地理编码
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *currentPlace = placemarks[0];
        self.navigationItem.title = currentPlace.name;
    }];
    //关闭定位
    //    [self.locationManager stopUpdatingLocation];
    
}

@end

